class MainHold {
	Main: egret.DisplayObject;

	constructor(Main: egret.DisplayObject) {
		this.Main = Main;
		Main.addEventListener(
			egret.TouchEvent.TOUCH_END,
			this.onTouchEndMain,
			this,
		);
	}

	private onTouchEndMain(e: egret.TouchEvent): void {
		if (this.holdParam) {
			this.releaseHold();
		}
	}

	holdParam: {
		hold: number;
		func: ()=>void;
		self: egret.DisplayObject;
		timeid: number;
	};

	// hold a while then can drag!
	addHoldDrag(ms: number, func: ()=>void, self: egret.DisplayObject): void {
		const param = {
			hold: new Date().getTime() + ms,
			func,
			self,
			timeid: 0,
		};
		this.holdParam = param;
		param.timeid = egret.setTimeout(
			() => {
				if (this.holdParam === param) {
					this.onHolded();
				}
			},
			this,
			ms,
		);
	}

	releaseHold(): void {
		this.holdParam = null;
	}

	onHolded(): void {
		const { func } = this.holdParam;
		const { self } = this.holdParam;
		this.releaseHold();
		func.apply(self);
	}

	checkHold(e: egret.TouchEvent): void {
		if (this.holdParam) {
			if (!isSelfOrParent(e.target, this.holdParam.self)) {
				console.log('this.releaseHold() not parent', e.target);
				this.releaseHold();
			}
			// console.log("checkHold",new Date().getTime(),this.holdParam.hold)
			// if (new Date().getTime() >= this.holdParam.hold){}
		}
	}
}
